package Forca.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class Velha extends JFrame {

	private static final long serialVersionUID = 1L;
	private JButton jButton0;
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JButton jButton4;
	private JButton jButton5;
	private JButton jButton6;
	private JButton jButton7;
	private JButton jButton8;
	private JLabel jLabel1;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public Velha() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJLabel1(), new Constraints(new Leading(72, 10, 10), new Leading(3, 12, 12)));
		add(getJButton8(), new Constraints(new Leading(80, 12, 12), new Leading(263, 23, 10, 10)));
		add(getJButton7(), new Constraints(new Leading(207, 12, 12), new Leading(263, 23, 12, 12)));
		add(getJButton6(), new Constraints(new Leading(349, 12, 12), new Leading(263, 23, 12, 12)));
		add(getJButton3(), new Constraints(new Leading(80, 12, 12), new Leading(151, 26, 12, 12)));
		add(getJButton4(), new Constraints(new Leading(207, 12, 12), new Leading(151, 26, 12, 12)));
		add(getJButton5(), new Constraints(new Leading(349, 12, 12), new Leading(151, 26, 12, 12)));
		add(getJButton2(), new Constraints(new Leading(349, 10, 10), new Leading(56, 26, 12, 12)));
		add(getJButton1(), new Constraints(new Leading(207, 10, 10), new Leading(56, 26, 12, 12)));
		add(getJButton0(), new Constraints(new Leading(80, 10, 10), new Leading(56, 26, 12, 12)));
		setSize(547, 389);
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setIcon(new ImageIcon(getClass().getResource("/IMGs/velha.jpg")));
			jLabel1.setVisible(false);
		}
		return jLabel1;
	}

	private JButton getJButton8() {
		if (jButton8 == null) {
			jButton8 = new JButton();
		}
		return jButton8;
	}

	private JButton getJButton7() {
		if (jButton7 == null) {
			jButton7 = new JButton();
		}
		return jButton7;
	}

	private JButton getJButton6() {
		if (jButton6 == null) {
			jButton6 = new JButton();
		}
		return jButton6;
	}

	private JButton getJButton5() {
		if (jButton5 == null) {
			jButton5 = new JButton();
		}
		return jButton5;
	}

	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
		}
		return jButton4;
	}

	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
		}
		return jButton3;
	}

	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton2ActionActionPerformed(event);
				}
			});
		}
		return jButton2;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton1ActionActionPerformed(event);
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					jButton0ActionActionPerformed(event);
				}
			});
		}
		return jButton0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				Velha frame = new Velha();
				frame.setDefaultCloseOperation(Velha.EXIT_ON_CLOSE);
				frame.setTitle("Velha");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0ActionActionPerformed(ActionEvent event) {
		jButton0.setText("X");
		jButton0.setEnabled(false);
		int maq = (int) ((int) 0+ (Math.random() * 8));
		if(maq == 8 && !(jButton8.getText().equals("X") || jButton8.getText().equals("O"))){
			jButton8.setText("O");
			jButton8.setEnabled(false);
		}else
		if(maq == 7 && !(jButton7.getText().equals("X") || jButton7.getText().equals("O"))){
			jButton7.setText("O");
			jButton7.setEnabled(false);
		}else
		if(maq == 6 && !(jButton6.getText().equals("X") || jButton6.getText().equals("O"))){
			jButton6.setText("O");
			jButton6.setEnabled(false);
		}else
		if(maq == 5 && !(jButton5.getText().equals("X") || jButton5.getText().equals("O"))){
			jButton5.setText("O");
			jButton5.setEnabled(false);
		}else
		if(maq == 4 && !(jButton4.getText().equals("X") || jButton4.getText().equals("O"))){
			jButton4.setText("O");
			jButton4.setEnabled(false);
		}else
		if(maq == 3 && !(jButton3.getText().equals("X") || jButton3.getText().equals("O"))){
			jButton3.setText("O");
			jButton3.setEnabled(false);
		}else
		if(maq == 2 && !(jButton2.getText().equals("X") || jButton2.getText().equals("O"))){
			jButton2.setText("O");
			jButton2.setEnabled(false);
		}else
		if(maq == 1 && !(jButton1.getText().equals("X") || jButton1.getText().equals("O"))){
			jButton1.setText("O");
			jButton1.setEnabled(false);
		}else
		if(maq == 0 && !(jButton0.getText().equals("X") || jButton0.getText().equals("O"))){
			jButton0.setText("O");
			jButton0.setEnabled(false);
			}
		
		if((jButton0.getText().equals(jButton1.getText()) && (jButton1.getText().equals(jButton2.getText())))){
			JOptionPane.showMessageDialog(null, "parabens");
		}
	}

	private void jButton1ActionActionPerformed(ActionEvent event) {
		jButton1.setText("X");
		jButton1.setEnabled(false);
	int maq = (int) ((int) 0+ (Math.random() * 8));
	if(maq == 8 && !(jButton8.getText().equals("X") || jButton8.getText().equals("O"))){
		jButton8.setText("O");
		jButton8.setEnabled(false);
	}else
	if(maq == 7 && !(jButton7.getText().equals("X") || jButton7.getText().equals("O"))){
		jButton7.setText("O");
		jButton7.setEnabled(false);
	}else
	if(maq == 6 && !(jButton6.getText().equals("X") || jButton6.getText().equals("O"))){
		jButton6.setText("O");
		jButton6.setEnabled(false);
	}else
	if(maq == 5 && !(jButton5.getText().equals("X") || jButton5.getText().equals("O"))){
		jButton5.setText("O");
		jButton5.setEnabled(false);
	}else
	if(maq == 4 && !(jButton4.getText().equals("X") || jButton4.getText().equals("O"))){
		jButton4.setText("O");
		jButton4.setEnabled(false);
	}else
	if(maq == 3 && !(jButton3.getText().equals("X") || jButton3.getText().equals("O"))){
		jButton3.setText("O");
		jButton3.setEnabled(false);
	}else
	if(maq == 2 && !(jButton2.getText().equals("X") || jButton2.getText().equals("O"))){
		jButton2.setText("O");
		jButton2.setEnabled(false);
	}else
	if(maq == 1 && !(jButton1.getText().equals("X") || jButton1.getText().equals("O"))){
		jButton1.setText("O");
		jButton1.setEnabled(false);
	}else
	if(maq == 0 && !(jButton0.getText().equals("X") || jButton0.getText().equals("O"))){
		jButton0.setText("O");
		jButton0.setEnabled(false);
		}
	}

	private void jButton2ActionActionPerformed(ActionEvent event) {
		jButton2.setText("X");
		jButton2.setEnabled(false);
		int maq = (int) ((int) 0+ (Math.random() * 8));
		if(maq == 8 && !(jButton8.getText().equals("X") || jButton8.getText().equals("O"))){
			jButton8.setText("O");
			jButton8.setEnabled(false);
		}else
		if(maq == 7 && !(jButton7.getText().equals("X") || jButton7.getText().equals("O"))){
			jButton7.setText("O");
			jButton7.setEnabled(false);
		}else
		if(maq == 6 && !(jButton6.getText().equals("X") || jButton6.getText().equals("O"))){
			jButton6.setText("O");
			jButton6.setEnabled(false);
		}else
		if(maq == 5 && !(jButton5.getText().equals("X") || jButton5.getText().equals("O"))){
			jButton5.setText("O");
			jButton5.setEnabled(false);
		}else
		if(maq == 4 && !(jButton4.getText().equals("X") || jButton4.getText().equals("O"))){
			jButton4.setText("O");
			jButton4.setEnabled(false);
		}else
		if(maq == 3 && !(jButton3.getText().equals("X") || jButton3.getText().equals("O"))){
			jButton3.setText("O");
			jButton3.setEnabled(false);
		}else
		if(maq == 2 && !(jButton2.getText().equals("X") || jButton2.getText().equals("O"))){
			jButton2.setText("O");
			jButton2.setEnabled(false);
		}else
		if(maq == 1 && !(jButton1.getText().equals("X") || jButton1.getText().equals("O"))){
			jButton1.setText("O");
			jButton1.setEnabled(false);
		}else
		if(maq == 0 && !(jButton0.getText().equals("X") || jButton0.getText().equals("O"))){
			jButton0.setText("O");
			jButton0.setEnabled(false);
			}
	}

}
